// posts.js
var util = require('../../utils/util.js');
var dbUtil = require('../../utils/db.js');
//获取应用实例
const app = getApp();

const db = wx.cloud.database();
var topic = null;
Page({
  data: {
    title: '话题详情',
    collectText:"收藏",
    detail: {},
    hidden: false,
    modalHidden: true,
    userInfo:null,
    comment:"",
    isAdmin: app.globalData.isAdmin,
    //删除评论标识
    delIndex:null,
    delId:null
  },

  onLoad: function (options) {

    let item = JSON.parse(options.data);

    this.setData({
      detail: item
    });

    this.fetchData(item._id);

    //设置是否为管理员
    this.setData({
      isAdmin: app.globalData.isAdmin
    });

    //浏览次数+1
    //dbUtil.inc("bbs_topic", options._id,"visit_count");
  },

  // 获取数据
  fetchData: function (_id) {
    var that = this;
    that.setData({
      hidden: false
    });
    var topic = this.data.detail;
    db.collection('common_comment').where({
      topic_id: _id
    }).get().then(res => {
      
      topic.replies = res.data.map(function (item) {
        item.create_at = util.getDateDiff(new Date(item.create_at));
        item.zanNum = item.ups.length;
        return item;
      })

      that.setData({ detail: topic });
    });

    setTimeout(function () {
      that.setData({ hidden: true });
    }, 300);
  },

  //发表回复
  submitComment: function (e) {

    //如果已经登陆,且还没有记录
    if (!this.logged && e.detail.userInfo) {
      this.setData({
        logged: true,
        userInfo: e.detail.userInfo
      });
      app.globalData.userInfo = e.detail.userInfo;
    }

    if (null == this.data.userInfo) {
      util.showErrorToast("不能匿名发表");
      return;
    }

    let that = this;

    if (null==this.data.comment || ""==this.data.comment) {
      util.showErrorToast('请输入回复内容')
      return false;
    }
  
    this.saveComment();
  },

  //保存贴子
  saveComment: function () {
    var newComment =
    {
      "author": {
        "loginname": this.data.userInfo.nickName,
        "avatar_url": this.data.userInfo.avatarUrl
      },
      "content": this.data.comment,
      "ups": [],
      "create_at": util.getCurrentTimeStr(),
      "topic_id": this.data.detail._id,
      "is_uped": false
    }
    var that = this;

    db.collection('common_comment').add({
      data: newComment,
      success: res => {
        newComment._id = res._id;
        newComment.create_at="刚刚发表";
        var tempTopic = that.data.detail;
        tempTopic.replies.push(newComment);
        that.setData({ detail: tempTopic, comment: "",});

        //回复个数+1
        //dbUtil.inc("bbs_topic", topic._id, "reply_count");

        wx.showToast({
          title: '发表成功'
        });
      },
      fail: e => {
        wx.showToast({
          title: '发布失败'
        });
      }
    })
  },

  bindInputValue(event) {
    let value = event.detail.value;
    //判断是否超过140个字符
    if (value && value.length > 140) {
      return;
    }
    this.setData({
      comment: event.detail.value,
    })
  },


  //删除评论
  delComment: function (e) {
    //下标
    var index = e.currentTarget.dataset.idx;
    var id = e.currentTarget.dataset.id;
    this.setData({ modalHidden: false, delIndex: index, delId: id });
  },
  
  // 关闭--模态弹窗
  cancelDel: function() {
    this.setData({ modalHidden: true });
  },
  // 确认--模态弹窗
  confirmDel: function() {
    var index = this.data.delIndex;
    var id = this.data.delId;
    db.collection('common_comment').doc(id).remove().then(res => {
      var tempTopic = this.data.detail;
      tempTopic.replies.splice(index, 1);
      this.setData({ detail: tempTopic, modalHidden: true });
      wx.showToast({ title: "删除成功" });
      //评论个数减1
      //dbUtil.updateProp("bbs_topic", topic._id, "reply_count", topic.reply_count - 1);
    });
  },
  //预览图片
  previewImage: function (e) {
    let tempFiles = [].concat(this.data.detail.headImg).concat(this.data.detail.imgs);

    wx.previewImage({
      current: e.currentTarget.dataset.url, // 当前显示图片的http链接
      urls: tempFiles // 需要预览的图片http链接列表
    })
  }

})
